package agents

import (
	"znet/zmodule"
	"znet/glog"
	"znet/leaf/gate"
	"znet/leaf/module"
	"znet/zgate"
	"znet/zconst/zcmd"
	"znet/zconst"
)

var (
	skeleton = zmodule.NewSkeleton()
	ChanRPC  = skeleton.ChanRPCServer
	Module	 = new(Agents)
)
type Agents struct {
	*module.Skeleton
}
func (this *Agents) OnInit() {
	this.Skeleton = skeleton
	skeleton.RegisterChanRPC(zconst.OnNewAgent, 	this.onNewAgent)
	skeleton.RegisterChanRPC(zconst.OnCloseAgent, 	this.onCloseAgent)
	skeleton.RegisterChanRPC(zcmd.CMD_ZNET_ALL, 	this.onPreMessage)
}
func (this *Agents) onNewAgent(args []interface{}) {
	a := args[0].(gate.Agent)
	glog.Infof("NewAgent : %v",a.RemoteAddr())

	data := zgate.NewZJson()
	data["a"]=1
	data["b"]=true
	data["c"]="走从@#爱联"
	a.WriteMsg(zgate.NewMessage(101,data))
}
func (this *Agents) onCloseAgent(args []interface{}) {
	a := args[0].(gate.Agent)
	glog.Infof("CloseAgent : %v",a.RemoteAddr())
}
func (this *Agents) onPreMessage(args []interface{}) {
	cmd 		:= args[0].(uint16)
	message 	:= args[1].(*zgate.Message)
	agent		:= args[2].(gate.Agent)
	this.onMessage(cmd,message,agent)
}
func (this *Agents) onMessage(cmd uint16,msg *zgate.Message,agent gate.Agent) {
	glog.Info(cmd)
	glog.Info(msg)
	glog.Info(agent)

	agent.WriteMsg(zgate.NewMessage(200,zgate.ZJson{"a":22}))
}
func (this *Agents) OnDestroy() {
}